from pptx import Presentation as Presentation
from pptx.util import Pt, Cm
from pptx.enum.shapes import MSO_SHAPE_TYPE
from pptx.dml.color import RGBColor
import os
import flet as fl
def main(page: fl.Page):
    page.title = "Create New PPT"
    page.padding=0
    def pick_files_result(e: fl.FilePickerResultEvent):
        selected_files.value = (
            ", ".join(map(lambda f: f.name, e.files)) if e.files else "Cancelled!"
        )
        selected_files.update()
    pick_files_dialog = fl.FilePicker(on_result=pick_files_result)
    selected_files = fl.Text()
    def get_directory_result(e: fl.FilePickerResultEvent):
        directory_path.value = e.path if e.path else "Cancelled!"
        directory_path.update()
    get_directory_dialog = fl.FilePicker(on_result=get_directory_result)
    directory_path = fl.Text()
    info=fl.Text(value="Ready",size=20,color=fl.colors.YELLOW_700,text_align=fl.TextAlign.CENTER)
    page.overlay.extend([pick_files_dialog, get_directory_dialog])
    page.window.width=300
    page.window.height=200
    TEXTBOX_POSITION = (Cm(1), Cm(0), Cm(15), Cm(2))
    LINE_POSITION = (Cm(1), Cm(2), Cm(31.867), Cm(0))
    IMAGE_POSITION = (Cm(1), Cm(2.5))
    IMAGE_SIZE = (Cm(16), Cm(16))  # 假设目标尺寸，实际根据图片比例调整
    def create_presentation(e):
        info.value="Running..."
        info.color=fl.colors.YELLOW_700
        page.update()
        presentation = Presentation()
        blank_layout = presentation.slide_layouts[6]
        folder_path = 'd:/pic/'
        if directory_path.value is not None and directory_path.value !="Cancelled!":
            folder_path = directory_path.value
        for root, dirs, files in os.walk(folder_path):
            for file_name in files:
                if file_name.endswith(".png"):
                    img_path = os.path.join(root, file_name)
                    slide = presentation.slides.add_slide(blank_layout)
                    textbox = slide.shapes.add_textbox(*TEXTBOX_POSITION)
                    tf = textbox.text_frame
                    tf.text = os.path.splitext(file_name)[0]  # 使用os.path.splitext来分离文件名和扩展名
                    font = tf.paragraphs[0].font
                    font.name = "SF Hello Light"
                    font.size = Pt(40)
                    line = slide.shapes.add_shape(MSO_SHAPE_TYPE.LINE, *LINE_POSITION)
                    line.line.color.rgb = RGBColor(0, 0, 0)
                    line.line.width = Pt(2)
                    aspect_ratio = 1.6
                    slide.shapes.add_picture(img_path, *IMAGE_POSITION, width=IMAGE_SIZE[0]*aspect_ratio, height=IMAGE_SIZE[1])
        presentation.save("d:/new.pptx")
        info.value="Done"
        info.color=fl.colors.GREEN
        page.update()
    page.add(
        fl.Text(value=""),
        fl.Row(
            [
                fl.ElevatedButton(
                    "Images Folder",
                    icon=fl.icons.FOLDER_OPEN,
                    on_click=lambda _: get_directory_dialog.get_directory_path(),
                    disabled=page.web,
                    style=fl.ButtonStyle(shape=fl.RoundedRectangleBorder(radius=1)),
                ),
                directory_path,
                
            ]
        ),
        fl.Column([
            fl.ElevatedButton(
                    "Start",
                    icon=fl.icons.COPY_ALL,
                    on_click=create_presentation,
                    disabled=page.web,
                    width=page.window.width,
                    style=fl.ButtonStyle(shape=fl.RoundedRectangleBorder(radius=1)),
                ),
            fl.Card(content=info,width=page.window.width,shape=fl.RoundedRectangleBorder(radius=1))
        ])
    )
fl.app(target=main)